package com.ibeeking.gmc.global.g.rest.service.impl;

import com.ibeeking.gmc.global.g.rest.service.ICampaignSceneService;
import com.ibeeking.gmc.global.g.rest.service.ICampaignSceneTenantService;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.gmc.global.api.common.dto.CampaignSceneDTO;
import com.ibeeking.gmc.global.service.common.bo.CampaignSceneBO;
import com.ibeeking.gmc.global.service.common.dos.CampaignSceneTenantDO;
import com.ibeeking.gmc.global.service.common.param.CampaignSceneTenantParam;
import com.ibeeking.gmc.global.service.common.query.CampaignSceneQuery;
import com.ibeeking.gmc.global.service.mapper.CampaignSceneTenantMapper;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CampaignSceneTenantServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-07-08 10:54
 **/
@Service
public class CampaignSceneTenantServiceImpl implements ICampaignSceneTenantService {

    @Resource
    private CampaignSceneTenantMapper campaignSceneTenantMapper;
    @Resource
    private ICampaignSceneService campaignSceneServiceImpl;

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Boolean configTenant(CampaignSceneTenantParam param) {
        campaignSceneTenantMapper.delete(new LambdaUpdateWrapper<>(CampaignSceneTenantDO.class)
                .eq(CampaignSceneTenantDO::getTenantId, param.getTenantId())
        );
        if (Collections3.isEmpty(param.getCampaignScenes())) {
            return true;
        }
        List<CampaignSceneTenantDO> saves = new ArrayList<>();
        param.getCampaignScenes().stream().distinct().forEach(id -> {
            CampaignSceneTenantDO campaignSceneTenantDO = new CampaignSceneTenantDO();
            campaignSceneTenantDO.setTenantId(param.getTenantId());
            campaignSceneTenantDO.setSceneId(id);
            campaignSceneTenantDO.setCreateBy(UserUtils.getUserId());
            saves.add(campaignSceneTenantDO);
        });
        campaignSceneTenantMapper.insertBatchSomeColumn(saves);
        return true;
    }

    @Override
    public List<String> scenesByTenantId(Integer tenantId) {
        List<CampaignSceneTenantDO> campaignSceneTenantDos = campaignSceneTenantMapper.selectList(new LambdaQueryWrapper<>(CampaignSceneTenantDO.class)
                .eq(CampaignSceneTenantDO::getTenantId, tenantId)
        );
        if (Collections3.isEmpty(campaignSceneTenantDos)) {
            return Collections3.emptyList();
        }
        return campaignSceneTenantDos.stream().map(s -> String.valueOf(s.getSceneId())).collect(Collectors.toList());
    }

    @Override
    public List<CampaignSceneDTO> queryCampaignScene(CampaignSceneQuery query) {
        if (null == query.getTenantId()) {
            return Collections3.emptyList();
        }
        //查询所有的活动场景
        List<CampaignSceneBO> scenes = campaignSceneServiceImpl.selectBySceneByType(query.getCampaignSceneType());
        if (Collections3.isEmpty(scenes)) {
            return Collections3.emptyList();
        }
        List<CampaignSceneTenantDO> campaignSceneTenantDos = campaignSceneTenantMapper.selectList(new LambdaQueryWrapper<>(CampaignSceneTenantDO.class)
                .eq(CampaignSceneTenantDO::getTenantId, query.getTenantId())
        );
        if (Collections3.isEmpty(campaignSceneTenantDos)) {
            return Collections3.emptyList();
        }
        List<Long> sceneIds = campaignSceneTenantDos.stream().map(CampaignSceneTenantDO::getSceneId).collect(Collectors.toList());
        return JsonUtils.jsonToList(scenes.stream().filter(s -> sceneIds.contains(s.getId())).collect(Collectors.toList()), CampaignSceneDTO.class);
    }
}
